package com.cooldatasoft.web.cms.person.dataProvider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.cooldatasoft.app.WOCApplication;
import com.cooldatasoft.data.Person;
import com.cooldatasoft.data.PersonPropertyValue;
import com.cooldatasoft.web.cms.person.model.PersonListItem;

public class PersonListPageDataProvider extends SortableDataProvider {

	protected List<PersonListItem> resultList=new ArrayList<PersonListItem>();
	
	public PersonListPageDataProvider(){
		// set default sort
		setSort("id", true);
		setSort("position",true);
		setSort("name",true);
		setSort("surname",true);
		
		listAll();
	}
	
	
	public List<PersonListItem> listAll() {
		List<Person> personList = WOCApplication.get().getPersonServiceProxy().getPersonList();
		Integer namePropertyId = 1;
		Integer surnamePropertyId = 2;
		Integer positionPropertyId = 3;
		if(personList!=null){
			for (Iterator<Person> iterator = personList.iterator(); iterator.hasNext();) {
				Person tbPerson = iterator.next();
				PersonListItem item = new PersonListItem();
				item.setId(tbPerson.getId());
				
				PersonPropertyValue nameValue = WOCApplication.get().getPersonServiceProxy().getPersonPropertyValue(tbPerson.getId(), namePropertyId);
				PersonPropertyValue surnameValue = WOCApplication.get().getPersonServiceProxy().getPersonPropertyValue(tbPerson.getId(), surnamePropertyId);
				PersonPropertyValue positionValue = WOCApplication.get().getPersonServiceProxy().getPersonPropertyValue(tbPerson.getId(), positionPropertyId);
				if(nameValue != null){
					item.setName(nameValue.getValue());
				}
				if(surnameValue != null){
					item.setSurname(surnameValue.getValue());
				}
				if(positionValue != null){
					item.setPosition(positionValue.getValue());
				}
				
				resultList.add(item);
			}
		}
		
		return resultList;
	}
	
	public Iterator<PersonListItem> iterator(int first, int count) {
		List<PersonListItem> newList = new ArrayList<PersonListItem>();
        newList.addAll(resultList.subList(first, first + count));

        final String sortColumn = this.getSort().getProperty();
        final boolean ascending = this.getSort().isAscending();

        Collections.sort(newList, new Comparator() {
			public int compare(Object entity1, Object entity2) {
				PropertyModel model1 = new PropertyModel(entity1, sortColumn);
                PropertyModel model2 = new PropertyModel(entity2, sortColumn);

                Object modelObject1 = model1.getObject();
                Object modelObject2 = model2.getObject();
                if( modelObject1==null ){
                	return -1;
                }else if( modelObject2 == null ){
                	return 1;
                }else if( modelObject1==null && modelObject2 == null){
                	return 0;
                }
                int compare = ((Comparable) modelObject1).compareTo(modelObject2);

                if (!ascending)
                    compare *= -1;

                return compare;
			}
        });

        return newList.iterator();
	}

	public IModel model(final Object object) {
		return new AbstractReadOnlyModel() {

			private static final long serialVersionUID = 1L;

			public Object getObject() {
                return object;
            }
        };
	}

	public int size() {
		return resultList.size();
	}

}
